package tau.dimes.visualization.applet;

import java.applet.Applet;

/**
 * Implements reading and parsing parameters from the containing HTML page.
 */
public class Parameters
{
	private static int interval = 2000;
	private static int window = 5;
	private static VisualizationType type = VisualizationType.Topology;
	private static String xmlrequest = "";
	private static boolean smooth = true;
	private static boolean showGrid = true;
	private static boolean aviRecord = false;
	private static int fps = 25;
	private static boolean fadeButKeep = false;
	private static int maxCircleRadius = 8;
	private static ZoomMode zoomMode = ZoomMode.Automatic;
	private static int minLatitude = -90;
	private static int maxLatitude = 90;
	private static int minLongitude = -180;
	private static int maxLongitude = 180;
	private static String bgImage = "WorldMap.jpg";

	/**
	 * Initializes the Parameters class.
	 * @param applet A reference to the containing applet.
	 */
	static void init(Applet applet)
	{
		initType(applet);
		initInterval(applet);
		initWindow(applet);
		initXmlRequest(applet);
		initSmooth(applet);
		initShowGrid(applet);
		initAviRecord(applet);
		initFps(applet);
		initFadeButKeep(applet);
		initMaxCircleRadius(applet);
		initZoomMode(applet);
		initMinLatitude(applet);
		initMaxLatitude(applet);
		initMinLongitude(applet);
		initMaxLongitude(applet);
		initBgImage(applet);
	}

	private static void initType(Applet applet)
	{
		try
		{
			String typeString = applet.getParameter("type");
			if (typeString != null)
				if (typeString.equals("topology"))
					type = VisualizationType.Topology;
				else if (typeString.equals("heatmap"))
					type = VisualizationType.HeatMap;
		}
		catch (Exception e)
		{
		}
	}

	private static void initInterval(Applet applet)
	{
		try
		{
			String intervalString = applet.getParameter("interval");
			if (intervalString != null)
				interval = Integer.parseInt(intervalString);
		}
		catch (Exception e)
		{
		}
	}

	private static void initWindow(Applet applet)
	{
		try
		{
			String windowString = applet.getParameter("window");
			if (windowString != null)
				window = Integer.parseInt(windowString);
		}
		catch (Exception e)
		{
		}
	}

	private static void initXmlRequest(Applet applet)
	{
		try
		{
			if (applet.getParameter("xmlrequest") != null)
				xmlrequest = applet.getParameter("xmlrequest");
		}
		catch (Exception e)
		{
		}
	}

	private static void initSmooth(Applet applet)
	{
		try
		{
			if (applet.getParameter("smooth") != null)
				smooth = Boolean.parseBoolean(applet.getParameter("smooth"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initShowGrid(Applet applet)
	{
		try
		{
			if (applet.getParameter("showGrid") != null)
				showGrid = Boolean.parseBoolean(applet.getParameter("showGrid"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initAviRecord(Applet applet)
	{
		try
		{
			if (applet.getParameter("aviRecord") != null)
				aviRecord = Boolean.parseBoolean(applet.getParameter("aviRecord"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initFps(Applet applet)
	{
		try
		{
			if (applet.getParameter("fps") != null)
				fps = Integer.parseInt(applet.getParameter("fps"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initFadeButKeep(Applet applet)
	{
		try
		{
			if (applet.getParameter("fadeButKeep") != null)
				fadeButKeep = Boolean.parseBoolean(applet.getParameter("fadeButKeep"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initMaxCircleRadius(Applet applet)
	{
		try
		{
			if (applet.getParameter("maxCircleRadius") != null)
				maxCircleRadius = Integer.parseInt(applet.getParameter("maxCircleRadius"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initZoomMode(Applet applet)
	{
		try
		{
			String zoomModeString = applet.getParameter("zoomMode");
			if (zoomModeString != null)
				if (zoomModeString.equals("auto"))
					zoomMode = ZoomMode.Automatic;
				else if (zoomModeString.equals("manual"))
					zoomMode = ZoomMode.Manual;
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initMinLatitude(Applet applet)
	{
		try
		{
			if (applet.getParameter("minLatitude") != null)
				minLatitude = Integer.parseInt(applet.getParameter("minLatitude"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initMaxLatitude(Applet applet)
	{
		try
		{
			if (applet.getParameter("maxLatitude") != null)
				maxLatitude = Integer.parseInt(applet.getParameter("maxLatitude"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initMinLongitude(Applet applet)
	{
		try
		{
			if (applet.getParameter("minLongitude") != null)
				minLongitude = Integer.parseInt(applet.getParameter("minLongitude"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initMaxLongitude(Applet applet)
	{
		try
		{
			if (applet.getParameter("maxLongitude") != null)
				maxLongitude = Integer.parseInt(applet.getParameter("maxLongitude"));
		}
		catch (Exception e)
		{
		}
	}
	
	private static void initBgImage(Applet applet)
	{
		try
		{
			if (applet.getParameter("bgImage") != null)
				bgImage = applet.getParameter("bgImage");
		}
		catch (Exception e)
		{
		}
	}

	/**
	 * Gets the visualization type.
	 * @return Visualization type.
	 */
	public static VisualizationType getType()
	{
		return type;
	}

	/**
	 * Gets the interval - time slot length in ms.
	 * @return Interval in ms.
	 */
	public static int getInterval()
	{
		return interval;
	}

	/**
	 * Gets the window size.
	 * @return Window size.
	 */
	public static int getWindow()
	{
		return window;
	}

	/**
	 * Gets the XML request string.
	 * @return XML request string.
	 */
	public static String getXmlrequest()
	{
		return xmlrequest;
	}

	/**
	 * Gets a value indicating whether to draw smooth graphics.
	 * @return - A value indicating whether to draw smooth graphics.
	 */
	public static boolean getSmooth()
	{
		return smooth;
	}

	/**
	 * Gets a value indicating whether to draw grid lines.
	 * @return - A value indicating whether to draw grid lines.
	 */
	public static boolean showGrid()
	{
		return showGrid;
	}

	/**
	 * Gets a value indicating whether to write a movie file.
	 * @return - A value indicating whether to write a movie file.
	 */
	public static boolean aviRecord()
	{
		return aviRecord;
	}

	/**
	 * Gets the number of frames per second.
	 * @return Number of frames per second.
	 */
	public static int getFps()
	{
		return fps;
	}
	
	/**
	 * Gets a value indicating whether arcs fade to partial opacity.
	 * @return - A value indicating whether arcs fade to partial opacity.
	 */
	public static boolean fadeButKeep()
	{
		return fadeButKeep;
	}
	
	/**
	 * Gets the maximum heat map circle radius.
	 * @return - The maximum circle radius.
	 */
	public static int maxCircleRadius()
	{
		return maxCircleRadius;
	}
	
	/**
	 * Gets the zoom mode.
	 * @return - The zoom mode.
	 */
	public static ZoomMode getZoomMode()
	{
		return zoomMode;
	}

	/**
	 * Gets the minimum latitude.
	 * @return - The minimum latitude.
	 */
	public static int getMinLatitude() 
	{
		return minLatitude;
	}

	/**
	 * Gets the maximum latitude.
	 * @return - The maximum latitude.
	 */
	public static int getMaxLatitude() 
	{
		return maxLatitude;
	}

	/**
	 * Gets the minimum longitude.
	 * @return - The minimum longitude.
	 */
	public static int getMinLongitude() 
	{
		return minLongitude;
	}

	/**
	 * Gets the maximum longitude.
	 * @return - The maximum longitude.
	 */
	public static int getMaxLongitude() 
	{
		return maxLongitude;
	}
	
	/**
	 * Gets the background image filename
	 * @return - The background image filename
	 */
	public static String getBgImage()
	{
		return bgImage;
	}
}